package com.masterclient.app.modules.cart.contract;

import com.masterclient.app.base.ProgressLoginDialog;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.masterclient.app.modules.cart.beans.ShopCartBean;
import com.eoner.baselibrary.bean.order.ConfirmOrderBean;
import org.json.JSONException;
import org.json.JSONObject;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
 * Created by TMVPHelper on 2017/07/18
 */
public class ShopCartPresenter extends ShopCartContract.Presenter {

    @Override
    public void getShopCartList(String cart_id, ProgressLoginDialog progressLoginDialog) {
        progressLoginDialog.show();
        progressLoginDialog.setDesc("加载中...");
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("cart_ids",cart_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<ShopCartBean> observable;
        observable = RetrofitUtil2.getInstance().getShopCartLists(body);

        Disposable disposable = toSubscribe(observable, (Consumer<ShopCartBean>) bean -> {
            if (bean != null && "0".equals(bean.getCode())) {
                mView.showShopCartList(bean);
            } else {
                mView.showShopCartListFailed(bean.getMsg());
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.showShopCartListFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void updateShopCartList(String cart_id, String num, ProgressLoginDialog progressLoginDialog) {
        progressLoginDialog.show();
        progressLoginDialog.setDesc("加载中...");
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("cart_id",cart_id);
            jsonObject.put("num",num);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<ShopCartBean> observable;
        observable = RetrofitUtil2.getInstance().updateShopCartLists(body);

        Disposable disposable = toSubscribe(observable, (Consumer<ShopCartBean>) bean -> {
            if (bean != null && "0".equals(bean.getCode())) {
                mView.updateShopCartList();
            } else {
                mView.updateShopCartListFailed(bean.getMsg());
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.updateShopCartListFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void collectShopCartList(String cart_id, ProgressLoginDialog progressLoginDialog) {
        progressLoginDialog.show();
        progressLoginDialog.setDesc("加载中...");
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("cart_ids",cart_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<ShopCartBean> observable;
        observable = RetrofitUtil2.getInstance().collectShopCartLists(body);

        Disposable disposable = toSubscribe(observable, (Consumer<ShopCartBean>) bean -> {
            if (bean != null && "0".equals(bean.getCode())) {
                mView.collectShopCartList();
            } else {
                mView.collectShopCartListFailed(bean.getMsg());
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.collectShopCartListFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void deleteShopCartList(String cart_id, ProgressLoginDialog progressLoginDialog) {
        progressLoginDialog.show();
        progressLoginDialog.setDesc("加载中...");
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("cart_ids",cart_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<ShopCartBean> observable;
        observable = RetrofitUtil2.getInstance().deleteShopCartLists(body);

        Disposable disposable = toSubscribe(observable, (Consumer<ShopCartBean>) bean -> {
            if (bean != null && "0".equals(bean.getCode())) {
                mView.deleteShopCartList();
            } else {
                mView.deleteShopCartListFailed(bean.getMsg());
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.deleteShopCartListFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void confirm_order(String buy_product, String buy_type,String sale_type, ProgressLoginDialog progressLoginDialog) {
        progressLoginDialog.show();
        progressLoginDialog.setDesc("加载中...");
        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject buyProduct = new JSONObject(buy_product);
            jsonObject.put("buy_product",buyProduct);
            jsonObject.put("buy_type",Integer.parseInt(buy_type));
            jsonObject.put("sale_type",sale_type);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<ConfirmOrderBean> observable = RetrofitUtil2.getInstance().confirmOrder(body);
        Disposable disposable = toSubscribe(observable, new Consumer<ConfirmOrderBean>() {
            @Override
            public void accept(@NonNull ConfirmOrderBean bean) throws Exception {
                if ("0".equals(bean.getCode())) {
                    mView.confirm_order_success(bean.getData(),buy_product);
                } else {
                    mView.confirm_order_failed(bean.getMsg());
                }
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                mView.confirm_order_failed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }
}